#include <iostream>

using namespace std;
#define int long long
int n , m;
const int N = 1e4 + 10 , M = 1e7 + 10;
int dp[M];
int t[N] , w[N];
signed main()
{
    cin >> m >> n;
    for(int i = 1;i <= n;i ++) cin >> t[i] >> w[i];
    for(int i = 1;i <= n;i ++)
    {
        for(int j = t[i];j <= m;j ++)
        {
                dp[j] = max(dp[j] , dp[j-t[i]] + w[i]);
        }
    }
    cout << dp[m] << endl;
    return 0;
}